﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using EMS.system.IEntityAppService.MasterData.UnitMeasureService;
using EMS.system.IEntityAppService.MasterData.UnitMeasureService.UnitMeasureDTOs;
using EMS.system.Entityies.MasterData.UnitMeasureModel;
using System.Threading.Tasks;
using CSRedis;

namespace EMS.system.Web.Controllers.MasterData.UnitMeasures
{
    public class UnitMeasureController : Controller
    {
        /// <summary>
        /// 计量单位服务类
        /// </summary>
        private readonly IUnitMeasureAppService _unitMeasureAppService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="unitMeasureAppService"></param>
        public UnitMeasureController(IUnitMeasureAppService unitMeasureAppService)
        {
            _unitMeasureAppService = unitMeasureAppService;
        }
        /// <summary>
        /// 计量单位显示视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UnitMeasureList()
        {
            return View();
        }
        /// <summary>
        /// 计量单位添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateUnitMeasure()
        {
            return View();
        }
        /// <summary>
        /// 计量单位修改视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateUnitMeasure(int id)
        {
            ViewBag.Id = id;
            return View();
        }
        /// <summary>
        /// 分页获取计量单位列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="unitMeasureCode"></param>
        /// <param name="unitMeasureName"></param>
        /// <returns></returns>
       
        [HttpGet]
        public async Task< JsonResult> GetUnitMeasureList(int page, int limit, string? unitMeasureCode, string? unitMeasureName)
        {
            var unitMeasure = await _unitMeasureAppService.GetUnitMeasureList(page, limit,unitMeasureCode,unitMeasureName);
            return Json(new { code = 0, data = unitMeasure.Queryable, count = unitMeasure.RowCount });
        }
        /// <summary>
        /// 添加计量单位
        /// </summary>
        /// <param name="unitMeasureDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> CreateUnitMeasure(UnitMeasureDTO unitMeasureDTO)
        {
            return await _unitMeasureAppService.CreateUnitMeasure(unitMeasureDTO);
        }
        /// <summary>
        /// 获取计量单位列表
        /// </summary>
        /// <param name="isPrimaryUnit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<UnitMeasureDTO>> GetUnitMeasure(bool isPrimaryUnit = true)
        {
            return await _unitMeasureAppService.GetUnitMeasure(isPrimaryUnit);
        }
        /// <summary>
        /// 删除计量单位
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> DeleteUnitMeasure(string ids)
        {
            return await _unitMeasureAppService.DeleteUnitMeasure(ids);

        }
        /// <summary>
        /// 获取计量单位主键Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<UnitMeasureDTO> GetUnitMeasureById(int id)
        {
            ViewBag.id = id;
            return await _unitMeasureAppService.GetUnitMeasureById(id);
        }
        /// <summary>
        /// 更新计量单位
        /// </summary>
        /// <param name="unitMeasureDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateUnitMeasure(UnitMeasureDTO unitMeasureDTO)
        {
            return await _unitMeasureAppService.UpdateUnitMeasure(unitMeasureDTO);
        }
    }
}
